package com.af.security.mgt.permisson;

import com.github.yitter.idgen.YitIdHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author : zhenyun.su
 * @since : 2018/9/17
 */

@Service
public class PermissionService {

    @Autowired
    private PermissionRepository permissionRepository;

    public List<Permission> findAll() {
        return permissionRepository.findAll();
    }

    @Transactional(readOnly = true)
    public List<Permission> findByPidOrderBySort(Long pid){
        return permissionRepository.findByPidOrderBySort(pid);
    }

    public List<Permission> getPermitByUserId(Long UserId){
        return permissionRepository.getPermitByUserId(UserId);
    }

    @Transactional
    public Permission save(Permission permission){
        if (permission.getId() != null){
            permission.setId(YitIdHelper.nextId());
        }
        return permissionRepository.save(permission);
    }
}
